﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LetsDealData.Entities;
using LetsDeal.Data;

namespace Administrator.Controllers
{
    public class CategoryController : Controller
    {
        // private LetsDealEntities db = new LetsDealEntities();
        private IGenericRepository genericRepository;

        public CategoryController()
        {
            genericRepository = new GenericRepository(new LetsDealDbContext());
        }

        public CategoryController(IGenericRepository genericRepository)
        {
            this.genericRepository = genericRepository;
        }

        //
        // GET: /Category/

        public ActionResult Index(int? selectedParent = 200)
        {
            // return View(db.Categories.ToList());
            var parents = genericRepository.Get<Category>(filter: c => c.Actived == true && c.Parent == 200);
            ViewBag.SelectedParent = new SelectList(parents, "Id", "Name", selectedParent);

            int parentId = selectedParent.GetValueOrDefault();
            var categories = genericRepository.Get<Category>(filter: c => c.Actived == true && c.Parent == parentId,
                orderBy: q => q.OrderBy(c => c.Order));
            return View(categories);
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id = 0)
        {
            // Category category = db.Categories.Find(id);
            Category category = genericRepository.GetById<Category>(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            var parents = genericRepository.Get<Category>(filter: c => c.Actived == true && c.Parent == 200);
            ViewBag.SelectedParent = new SelectList(parents, "Id", "Name", 200);

            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                // db.Categories.Add(category);
                // db.SaveChanges();
                genericRepository.Create<Category>(category);
                genericRepository.SaveChanged();

                return RedirectToAction("Index");
            }

            return View(category);
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id = 0)
        {
            // Category category = db.Categories.Find(id);
            Category category = genericRepository.GetById<Category>(id);

            var parents = genericRepository.Get<Category>(filter: c => c.Actived == true && c.Parent == 200);
            ViewBag.SelectedParent = new SelectList(parents, "Id", "Name", category.Parent);

            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                // db.Entry(category).State = EntityState.Modified;
                // db.SaveChanges();
                genericRepository.Update<Category>(category);
                genericRepository.SaveChanged();

                return RedirectToAction("Index");
            }
            return View(category);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(int id = 0)
        {
            // Category category = db.Categories.Find(id);
            Category category = genericRepository.GetById<Category>(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            // Category category = db.Categories.Find(id);
            // db.Categories.Remove(category);
            // db.SaveChanges();
            Category category = genericRepository.GetById<Category>(id);
            genericRepository.Delete<Category>(category);
            genericRepository.SaveChanged();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            // db.Dispose();
            base.Dispose(disposing);
        }
    }
}